<?php
class CronComponent extends Component
{
    var $controller;
	function main()
	{
		$this->update_project();
		$this->feed();		
		$this->affiliate();		
	}
    function update_project()
    {
        App::import('Model', 'Project');
        $this->Project = new Project();
        $projects = $this->Project->find('all', array(
            'conditions' => array(
                'Project.project_status_id' => array(
					ConstProjectStatus::OpenForFunding,
					ConstProjectStatus::GoalReached,
					ConstProjectStatus::ReserveAmountReached,
                ) ,
            ) ,
            'contain' => array(
                'User' => array(
                    'fields' => array(
                        'User.id',
                        'User.username',
                        'User.email'
                    )
                ) ,
                'ProjectFund' => array(
                    'fields' => array(
                        'ProjectFund.user_id'
                    )
                ) ,
                'ProjectFollower' => array(
                    'fields' => array(
                        'ProjectFollower.user_id'
                    )
                ) ,
            ) ,
            'fields' => array(
                'Project.id',
                'Project.user_id',
                'Project.name',
                'Project.slug',
                'Project.needed_amount',
                'Project.collected_amount',
                'Project.is_system_flagged',
                'Project.project_funding_end_date',
                'Project.is_allow_over_funding',
				'Project.project_status_id'
            ) ,
            'recursive' => 2
        ));
        $this->Project->Behaviors->detach('Logable');
        foreach($projects as $project) {
            if ($project['Project']['collected_amount'] >= $project['Project']['needed_amount'] && empty($project['Project']['is_allow_over_funding'])) {
                $this->Project->updateAll(array(
                    'Project.project_status_id' => ConstProjectStatus::FundingClosed,
                    'Project.project_fund_goal_reached_date' => "'" . date('Y-m-d H:i:s') . "'",
                ) , array(
                    'Project.id' => $project['Project']['id']
                ));
                // sending Mail to owner
                $owner_user[] = $project['Project']['user_id'];
                $return_owner_lists = $this->Project->_sendAlertOnProjectSuccessful(array_unique($owner_user) , $project, ConstProjectUsers::Owner);
                // send mail to backed
                $pledged_users = Set::extract('/ProjectFund/user_id', $project);
                $pledged_user_lists = $this->Project->_sendAlertOnProjectSuccessful(array_unique($pledged_users) , $project, ConstProjectUsers::Backer, $return_owner_lists);
                // send mail to followers
                $follow_users = Set::extract('/ProjectFollower/user_id', $project);
                $this->Project->_sendAlertOnProjectSuccessful(array_unique($follow_users) , $project, ConstProjectUsers::Follower, $pledged_user_lists);
                //Send mail to admin
                $this->Project->_sendAlertOnProjectStatusToAdmin($project, $project['Project']['project_status_id'], ConstProjectStatus::FundingClosed);
                if (!($project['Project']['is_system_flagged']) || ($project['Project']['is_system_flagged'] && Configure::read('Project.is_allow_fund_capture_flagged_projects'))) {
                    $this->Project->_executepay($project['Project']['id']);
                }
            }
             else if($project['Project']['collected_amount'] >= $project['Project']['needed_amount'] && strtotime($project['Project']['project_funding_end_date'] . ' 23:55:59') <= strtotime(date('Y-m-d H:i:s')) && !empty($project['Project']['is_allow_over_funding']))
             {
               $this->Project->updateAll(array(
                        'Project.project_status_id' => ConstProjectStatus::FundingClosed
                    ) , array(
                        'Project.id' => $project['Project']['id']
                    ));
             }
             else {
                if (strtotime($project['Project']['project_funding_end_date'] . ' 23:55:59') <= strtotime(date('Y-m-d H:i:s'))) {
                    $this->Project->updateAll(array(
                        'Project.project_status_id' => ConstProjectStatus::FundingExpired
                    ) , array(
                        'Project.id' => $project['Project']['id']
                    ));
                    // sending Mail to owner
                    $owner_user[] = $project['Project']['user_id'];
                    $return_owner_lists = $this->Project->_sendAlertOnProjectExpired(array_unique($owner_user) , $project, ConstProjectUsers::Owner);
                    // send mail to backed
                    $pledged_users = Set::extract('/ProjectFund/user_id', $project);
                    $pledged_user_lists = $this->Project->_sendAlertOnProjectExpired(array_unique($pledged_users) , $project, ConstProjectUsers::Backer, $return_owner_lists);
                    // send mail to followers
                    $follow_users = Set::extract('/ProjectFollower/user_id', $project);
                    $this->Project->_sendAlertOnProjectExpired(array_unique($follow_users) , $project, ConstProjectUsers::Follower, $pledged_user_lists);
                    // Send mail to admin
                    $this->Project->_sendAlertOnProjectStatusToAdmin($project, $project['Project']['project_status_id'], ConstProjectStatus::FundingExpired);
                    $this->Project->_refund($project['Project']['id']);
                }
            }
        }
        $this->Project->_updateCityProjectCount();
    }
    function feed()
    {
        App::import('Model', 'Project');
        $this->Project = new Project();
		$this->Project->Behaviors->detach('Logable');
        $projects = $this->Project->find('all', array(
            'conditions' => array(
                'Project.feed_url !=' => Null
            ) ,
            'fields' => array(
                'Project.id',
                'Project.feed_url',
            ) ,
            'recursive' => - 1
        ));
        foreach($projects as $project) {
            if (!empty($project['Project']['feed_url'])) {
                $this->Project->ProjectFeed->rss_feed($project['Project']['feed_url'], '', $project['Project']['id']);
            }
        }
    }
	function affiliate()
	{
		if (Configure::read('affiliate.is_enabled')) {
			App::import('Model', 'Affiliate');
			$this->Affiliate = new Affiliate();
			$this->Affiliate->update_affiliate_status();
		}
	}
	public function clear_permanent_cache()
    {
        $cacheDir = array(
            CACHE . DS . 'views' . DS . 'user',
            CACHE . DS . 'views' . DS . 'public'
        );
        foreach($cacheDir as $dir) {
            $r = array();
            $dh = @opendir($dir);
            if ($dh) {
                while (($fname = readdir($dh)) !== false) {
                    if ($fname != '.' && $fname != '..') {
                        $r[$fname] = fileatime($dir . DS . $fname);
                    }
                }
                closedir($dh);
                if (!empty($r)) {
                    asort($r);
                    $r = array_keys($r);
                    if (sizeof($r) >= 100) {
                        for ($i = 100; $i < sizeof($r); $i++) {
                            @unlink($dir . DS . $r[$i]);
                        }
                    }
                }
            }
        }
        @unlink(APP . WEBROOT_DIR . DS . 'index.html');
    }
}
?>